<?php

/*
 * Copyright 2017 zhc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Description of books
 * @package Library
 * 
 * @author Zhu Chen <zhuchen@greatqq.com>
 */
class Book {
    /**
     * The book action is rent out
     */
    const BOOK_ACTION_RENTOUT = 1;
    /**
     * THe book action is return back
     */
    const BOOK_ACTION_RETURN =2;
    
    /**
     * The book is disposed from the library
     */
    const BOOK_ACTION_DELETE =3;
    /**
     * The book unique id
     * @var int 
     */
    public $id;
    
    /**
     * The book name
     * @var string
     */
    public $name;
    
    /**
     * The book publisher
     * @var string 
     */
    public $publisher;
    
    /**
     * The 13 number isbn code
     * @var string  
     */
    public $isbn;
    
    /**
     * The book price
     * @var float 
     */
    public $price;
    
    /**
     * The description of the book
     * @var string  
     */
    public $description;
    
    /**
     * Rent out a book
     * @param int $user
     * @return int 0 for success 
     * @throws InvalidArgumentException
     */
    public function Rentout(int $user)
    {
        $result = DB::select('books', '`id` = '.$this->id,'`id`',true);
        if (!$result) {
            throw new InvalidArgumentException('The book cannot be found');
        }
        //Check whether is rent out;
        if ($result['rent'] != 0) {
            return $result['rent'];
        }
        DB::update('books', ['rent'=>$user], '`id` = '.$this->id);
        DB::insert('bookhistory',['book'=>$this->id,'action'=>Book::BOOK_ACTION_RENTOUT,'user'=>$user,'time'=>time()]);
        return 0;
    }
    
    /**
     * Return a book
     * @return int
     * @throws InvalidArgumentException
     */
    public function ReturnBook(int $user)
    {
        $result = DB::select('books', '`id` = '.$this->id,'`id`',true);
        if (!$result) {
            throw new InvalidArgumentException('The book cannot be found');
        }
        DB::update('books', ['rent'=>0], '`id` = '.$this->id);
        DB::insert('bookhistory',['book'=>$this->id,'action'=>Book::BOOK_ACTION_RETURN,'user'=>$user,'time'=>time()]);
        return 1;
    }
    
    /**
     * Reterive the history of a book
     * @return type
     */
    public function ShowHistory()
    {
        $result = DB::select('bookhistory','`book`='.$this->id);
        return $result;
    }
    
    /**
     * Establish the book by using the bookid
     * @param int $id The book ID.
     * @throws InvalidArgumentException throws when book cannot be found.
     */
    public function __construct(int $id)    {
        $result = DB::select('books', '`id` = '.$id,'`id`',true);
        if (!$result) {
            throw new InvalidArgumentException('The book cannot be found');
        }
        $this->id = $id;
        $this->name = $result['name'];
        $this->isbn = $result['isbn'];
        $this->description = $result['description'];
        $this->price = floatval($result['price']);
    }
    
    /**
     * Convert the book ID to the bookname
     * @param int $id The id of the book
     * @return boolean|string failed with false success with book name.
     */
    public static function bid2name(int $id)    {
        $result = DB::select('books', '`id` = '.$id,'`id`',true);
        if (!$result) {
            return FALSE;
        }
        return $result['name'];
    }
    
    
}
